package com.union.core.workModel.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hnjowe.webdb.DBOperation;
import com.union.core.workModel.dao.ModelBonusDao;
import com.union.core.workModel.model.ModelBonus;
import com.union.core.workModel.model.ModelMember;

public class ModelBonusDaoImpl implements ModelBonusDao {
	private DBOperation opera;
	private ModelBonus modelBonus;
	private static Logger log = Logger.getLogger(ModelBonusDaoImpl.class.getName());
	@Override
	public boolean addBonus(ModelBonus modelBonus) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "insert into model_bonus(" +
					"model_id,model_name,bonus_year," +
					"bonus_newyear,bonus_diffculty,bonus_low_subsidy," +
					"bonus_ill,bonus_famer,bonus_total,bonus_live,model_type) values " +
					"(?,?,?,?,?,?,?,?,?,?,?)";
			opera.prepStatment(sql);
			opera.setInt(1, modelBonus.getModel_id());
			opera.setString(2, modelBonus.getModel_name());
			opera.setString(3, modelBonus.getBonus_year());
			opera.setFloat(4, modelBonus.getBonus_newyear());
			opera.setFloat(5, modelBonus.getBonus_diffculty());
			opera.setFloat(6, modelBonus.getBonus_low_subsidy());
			opera.setFloat(7, modelBonus.getBonus_ill());
			opera.setFloat(8, modelBonus.getBonus_famer());
			opera.setFloat(9, modelBonus.getBonus_total());
			opera.setFloat(10, modelBonus.getBonus_live());
			opera.setString(11, modelBonus.getModel_type());
			opera.execute();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}

	@Override
	public boolean delBonus(int bonusId) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "delete from model_bonus where bonus_id=?";
			opera.prepStatment(sql);
			opera.setInt(1, bonusId);
			opera.execute();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}

	@Override
	public boolean editBonus(ModelBonus modelBonus) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "update model_bonus set model_name=?,bonus_year=?," +
					"bonus_newyear=?,bonus_diffculty=?,bonus_low_subsidy=?," +
					"bonus_ill=?,bonus_famer=?,bonus_total=?,bonus_live=?,model_type=? where bonus_id=?";
			opera.prepStatment(sql);
			opera.setString(1, modelBonus.getModel_name());
			opera.setString(2, modelBonus.getBonus_year());
			opera.setFloat(3, modelBonus.getBonus_newyear());
			opera.setFloat(4, modelBonus.getBonus_diffculty());
			opera.setFloat(5, modelBonus.getBonus_low_subsidy());
			opera.setFloat(6, modelBonus.getBonus_ill());
			opera.setFloat(7, modelBonus.getBonus_famer());
			opera.setFloat(8, modelBonus.getBonus_total());
			opera.setFloat(9, modelBonus.getBonus_live());
			opera.setString(10, modelBonus.getModel_type());
			opera.setInt(11, modelBonus.getBonus_id());
			opera.executeUpdate();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}

	@Override
	public List<ModelBonus> queryBonusList(int modelId) {
		List<ModelBonus> bonusList = new ArrayList<ModelBonus>();
		try{
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "select "
					+ "bonus_id,model_id,model_name,bonus_year," +
					"bonus_newyear,bonus_diffculty,bonus_low_subsidy," +
					"bonus_ill,bonus_famer,bonus_total,bonus_live,model_type "
					+ "from model_bonus "
					+ "where model_id=?";
			opera.prepStatment(sql);
			opera.setInt(1, modelId);
			ResultSet res = opera.executeQuery();// 执行上面语句
			int i = 0;
			
			while (res.next()) {
				modelBonus = new ModelBonus();
				modelBonus.setBonus_id(res.getInt(1));
				modelBonus.setModel_id(res.getInt(2));
				modelBonus.setModel_name(res.getString(3));
				modelBonus.setBonus_year(res.getString(4));
				modelBonus.setBonus_newyear(res.getFloat(5));
				modelBonus.setBonus_diffculty(res.getFloat(6));
				modelBonus.setBonus_low_subsidy(res.getFloat(7));
				modelBonus.setBonus_ill(res.getFloat(8));
				modelBonus.setBonus_famer(res.getFloat(9));
				modelBonus.setBonus_total(res.getFloat(10));
				modelBonus.setBonus_live(res.getFloat(11));
				modelBonus.setModel_type(res.getString(12));
				bonusList.add(i, modelBonus);
				modelBonus=null;
				i++;
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return bonusList;
	}

	@Override
	public ModelBonus queryById(int bonusId) {
		ModelBonus modelBonus = null;
		try {
			opera = new DBOperation();
			opera.transcationBegin();

			String sql = "select bonus_id,model_id,model_name,bonus_year," +
					"bonus_newyear,bonus_diffculty,bonus_low_subsidy," +
					"bonus_ill,bonus_famer,bonus_total,bonus_live "
					+ "from model_bonus where  bonus_id=?";
			opera.prepStatment(sql);
			opera.setInt(1, bonusId);
			ResultSet res = opera.executeQuery();
			if (res.next()) {
				modelBonus = new ModelBonus();
				modelBonus.setBonus_id(res.getInt(1));
				modelBonus.setModel_id(res.getInt(2));
				modelBonus.setModel_name(res.getString(3));
				modelBonus.setBonus_year(res.getString(4));
				modelBonus.setBonus_newyear(res.getFloat(5));
				modelBonus.setBonus_diffculty(res.getFloat(6));
				modelBonus.setBonus_low_subsidy(res.getFloat(7));
				modelBonus.setBonus_ill(res.getFloat(8));
				modelBonus.setBonus_famer(res.getFloat(9));
				modelBonus.setBonus_total(res.getFloat(10));
				modelBonus.setBonus_live(res.getFloat(11));
				modelBonus.setModel_type(res.getString(12));
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return modelBonus;
	}

	@Override
	public boolean delBonusList(int modelId) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			String sql = "delete from model_bonus where model_id=?";
			opera.prepStatment(sql);
			opera.setInt(1, modelId);
			opera.execute();
			result = opera.transcationCommit();
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;
	}

}
